﻿<?xml version="1.0" encoding="utf-8"?>

<scope
	xmlns="http://metalx.org/Program"
	xmlns:cpu="http://metalx.org/Mos/6502/Operators"
	xmlns:vid="http://metalx.org/Commodore64/Video"
	xmlns:sys="http://metalx.org/Commodore64/System"
	xmlns:key="http://metalx.org/Commodore64/Keyboard"
	xmlns:kernel="http://metalx.org/Commodore64/Kernel"
	xmlns:mm="http://metalx.org/C64/Functions/MemoryManager"
	xmlns:str="http://metalx.org/C64/Functions/String"
	xmlns:cls="http://metalx.org/C64/Functions/Class">
	
	<!--Define Memory Address Labels-->
	<sys:CpuPortDirectionControl id="cpuPortDirectionControl"/>
	<sys:CpuRomBankControl id="cpuRomBankControl"/>
	<vid:BorderColor id="borderColor"/>
	<vid:CharacterSettings id="characterSettings"/>
	
	<!--Disable CPU Interrupts-->
	<cpu:SetInterruptDisableFlag/>
	
	<!--Copy ROM to RAM-->
	<cpu:CopyImmediate8ToXIndex/>
	<hex>00</hex>

	<label id="copyRom"/>
	
	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<hex>8000</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>1000</hex>

	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<hex>8100</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>1100</hex>

	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<hex>8200</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>1200</hex>

	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<hex>8300</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>1300</hex>

	<cpu:IncrementXIndex/>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="copyRom" type="Relative8"/>

	<cpu:JumpToImmediate16Address/>
	<addressOf ref="start" type="Absolute16"/>
	
	<label id="start"/>
	
	<!--Disable CIA Interrupts-->
	<cpu:CopyImmediate8ToAccumulator/>
	<hex>7f</hex>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>dc0d</hex>
	
	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>dd0d</hex>
	
	<!--Setup CIA 1 Chip (Keyboard)-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>11111111</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>dc02</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00000000</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>dc03</hex>

	<!--Setup VIC Chip-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00011011</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d011</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<hex>0f</hex>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d012</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00001000</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d016</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>10000001</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d01a</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00010100</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d018</hex>
	
	<!--Clear Interrupts-->
	<cpu:CopyImmediate16AddressToAccumulator/>
	<hex>dc0d</hex>

	<cpu:CopyImmediate16AddressToAccumulator/>
	<hex>dd0d</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00001111</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d019</hex>

	<!--Clear Screen-->
	<cpu:CopyImmediate8ToXIndex/>
	<hex>00</hex>

	<cpu:CopyXIndexToImmediate16Address/>
	<hex>d021</hex>

	<cpu:CopyXIndexToImmediate16Address/>
	<hex>d020</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<hex>20</hex>

	<label id="nextCharacter"/>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0400</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0500</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0600</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0700</hex>

	<cpu:IncrementXIndex/>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="nextCharacter" type="Relative8"/>

	<cpu:CopyImmediate8ToAccumulator/>
	<hex>01</hex>

	<label id="nextCharacter2"/>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>d800</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>d900</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>da00</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>db00</hex>

	<cpu:IncrementXIndex/>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="nextCharacter2" type="Relative8"/>
	
	<!--Enable Lower Case Characters-->
	<cpu:CopyImmediate16AddressToAccumulator/>
	<addressOf ref="characterSettings" type="Absolute16"/>

	<cpu:OrAccumulatorWithImmediate8/>
	<binary>00000010</binary>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<addressOf ref="characterSettings" type="Absolute16"/>

	<!--Disable ROM Banks-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>11101111</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuPortDirectionControl" type="Absolute16Low8"/>

	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00100000</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuRomBankControl" type="Absolute16Low8"/>

	<!--Setup Interrupt Handlers-->
	<cpu:CopyImmediate8ToAccumulator/>
	<addressOf ref="interruptHandler" type="Absolute16Low8"/>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>fffe</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<addressOf ref="interruptHandler" type="Absolute16High8"/>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>ffff</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<addressOf ref="resetHandler" type="Absolute16Low8"/>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>fffa</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<addressOf ref="resetHandler" type="Absolute16High8"/>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>fffb</hex>

	<!--Initialize Keyboard-->
	<cpu:CopyImmediate8ToXIndex/>
	<byte>15</byte>

	<cpu:CopyImmediate8ToAccumulator/>
	<hex>ff</hex>

	<label id="initializeKeyboard"/>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0200</hex>

	<cpu:DecrementXIndex/>

	<cpu:BranchToRelative8IfPositive/>
	<addressOf ref="initializeKeyboard" type="Relative8"/>
	
	<!--Set Cursor Position-->
	<cpu:CopyImmediate8ToAccumulator/>
	<hex>00</hex>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<hex>fb</hex>

	<cpu:CopyImmediate8ToAccumulator/>
	<hex>04</hex>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<hex>fc</hex>

	<!--Enable Interrupts-->
	<cpu:ClearInterruptDisableFlag/>
	
	<!--Show Prompt-->
	<label id="showPrompt"/>

	<cpu:CopyImmediate8ToAccumulator/>
	<string>&gt;</string>

	<cpu:CopyImmediate8ToYIndex/>
	<byte>0</byte>

	<cpu:CopyAccumulatorToImmediate8PointerPlusYIndexAddress/>
	<hex>fb</hex>

	<cpu:IncrementImmediate8Address/>
	<hex>fb</hex>

	<label id="waitForKeypress"/>

	<!--Store Keys-->
	<cpu:CopyImmediate8ToXIndex/>
	<byte>7</byte>

	<label id="storeKeys"/>

	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<hex>0200</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0208</hex>

	<cpu:DecrementXIndex/>

	<cpu:BranchToRelative8IfPositive/>
	<addressOf ref="storeKeys" type="Relative8"/>

	<cpu:SetInterruptDisableFlag/>

	<!--Enable I/O Access-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00100101</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuRomBankControl" type="Absolute16Low8"/>

	<!--Read Keys-->
	<cpu:CopyImmediate8ToXIndex/>
	<byte>7</byte>

	<label id="readKeys"/>

	<cpu:CopyImmediate16PlusXIndexAddressToAccumulator/>
	<addressOf ref="keyboardRowMasks" type="Absolute16"/>

	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>dc00</hex>

	<cpu:CopyImmediate16AddressToAccumulator/>
	<hex>dc01</hex>

	<cpu:CopyAccumulatorToImmediate16PlusXIndexAddress/>
	<hex>0200</hex>
	
	<cpu:DecrementXIndex/>

	<cpu:BranchToRelative8IfPositive/>
	<addressOf ref="readKeys" type="Relative8"/>
	
	<!--Disable I/O Access-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00100000</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuRomBankControl" type="Absolute16Low8"/>

	<cpu:ClearInterruptDisableFlag/>

	<!--Find Keys-->
	<cpu:CopyImmediate8ToXIndex/>
	<byte>32</byte>

	<label id="findKeys"/>
	
	<cpu:CopyImmediate16PlusXIndexAddressToYIndex/>
	<addressOf ref="keyboardColumns" type="Absolute16"/>

	<cpu:CopyImmediate16PlusYIndexAddressToAccumulator/>
	<hex>0200</hex>

	<cpu:AndAccumulatorWithImmediate16PlusXIndexAddress/>
	<addressOf ref="keyboardRows" type="Absolute16"/>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="notPressed" type="Relative8"/>

	<cpu:ExclusiveOrAccumulatorWithImmediate16PlusYIndexAddress/>
	<hex>0208</hex>

	<cpu:AndAccumulatorWithImmediate16PlusXIndexAddress/>
	<addressOf ref="keyboardRows" type="Absolute16"/>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="keyPressed" type="Relative8"/>

	<label id="notPressed"/>

	<cpu:DecrementXIndex/>

	<cpu:BranchToRelative8IfPositive/>
	<addressOf ref="findKeys" type="Relative8"/>

	<cpu:JumpToImmediate16Address/>
	<addressOf ref="waitForKeypress" type="Absolute16"/>
	
	<label id="keyPressed"/>

	<cpu:CopyXIndexToAccumulator/>

	<cpu:CopyImmediate8ToYIndex/>
	<byte>0</byte>

	<cpu:CopyAccumulatorToImmediate8PointerPlusYIndexAddress/>
	<hex>fb</hex>

	<cpu:IncrementImmediate8Address/>
	<hex>fb</hex>

	<cpu:BranchToRelative8IfNotZero/>
	<addressOf ref="notCarry" type="Relative8"/>

	<cpu:IncrementImmediate8Address/>
	<hex>fc</hex>

	<label id="notCarry"/>

	<cpu:JumpToImmediate16Address/>
	<addressOf ref="waitForKeypress" type="Absolute16"/>

	<cpu:NoOperation/>
	
	<!--Reset Handler-->
	<label id="resetHandler"/>

	<cpu:PullAccumulatorFromStack/>
	<cpu:PullAccumulatorFromStack/>
	<cpu:PullAccumulatorFromStack/>

	<cpu:JumpToImmediate16Address/>
	<addressOf ref="start" type="Absolute16"/>

	<!--Interrupt Handler-->
	<label id="interruptHandler"/>

	<cpu:PushAccumulatorToStack/>

	<!--Enable I/O Access-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00100101</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuRomBankControl" type="Absolute16Low8"/>

	<cpu:CopyImmediate16AddressToAccumulator/>
	<hex>d019</hex>

	<cpu:AndAccumulatorWithImmediate8/>
	<binary>00000001</binary>

	<cpu:BranchToRelative8IfZero/>
	<addressOf ref="returnFromInterrupt" type="Relative8"/>
	
	<cpu:CopyAccumulatorToImmediate16Address/>
	<hex>d019</hex>
	
	<label id="returnFromInterrupt"/>

	<!--Disable ROM Banks-->
	<cpu:CopyImmediate8ToAccumulator/>
	<binary>00100000</binary>

	<cpu:CopyAccumulatorToImmediate8Address/>
	<addressOf ref="cpuRomBankControl" type="Absolute16Low8"/>

	<cpu:PullAccumulatorFromStack/>
	
	<cpu:ReturnFromInterrupt/>

	<!--Keyboard Row Masks-->
	<label id="keyboardRowMasks"/>
	<hex>fe</hex>
	<hex>fd</hex>
	<hex>fb</hex>
	<hex>f7</hex>
	<hex>ef</hex>
	<hex>df</hex>
	<hex>bf</hex>
	<hex>7f</hex>
	
	<!--Keyboard Column Table-->
	<label id="keyboardColumns"/>
	<byte>5</byte>
	<byte>1</byte>
	<byte>3</byte>
	<byte>2</byte>
	<byte>2</byte>
	<byte>1</byte>
	<byte>2</byte>
	<byte>3</byte>

	<byte>3</byte>
	<byte>4</byte>
	<byte>4</byte>
	<byte>4</byte>
	<byte>5</byte>
	<byte>4</byte>
	<byte>4</byte>
	<byte>4</byte>

	<byte>5</byte>
	<byte>7</byte>
	<byte>2</byte>
	<byte>1</byte>
	<byte>2</byte>
	<byte>3</byte>
	<byte>3</byte>
	<byte>1</byte>

	<byte>2</byte>
	<byte>3</byte>
	<byte>1</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>

	<byte>7</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>

	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>

	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>

	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	<byte>5</byte>
	
	<!--Keyboard Row Table-->
	<label id="keyboardRows"/>
	<binary>01000000</binary>
	<binary>00000100</binary>
	<binary>00010000</binary>
	<binary>00010000</binary>
	<binary>00000100</binary>
	<binary>01000000</binary>
	<binary>00100000</binary>
	<binary>00000100</binary>

	<binary>00100000</binary>
	<binary>00000010</binary>
	<binary>00000100</binary>
	<binary>00100000</binary>
	<binary>00000100</binary>
	<binary>00010000</binary>
	<binary>10000000</binary>
	<binary>01000000</binary>

	<binary>00000010</binary>
	<binary>01000000</binary>
	<binary>00000010</binary>
	<binary>00100000</binary>
	<binary>01000000</binary>
	<binary>01000000</binary>
	<binary>10000000</binary>
	<binary>00000010</binary>

	<binary>10000000</binary>
	<binary>00000010</binary>
	<binary>00010000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>

	<binary>00010000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>

	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>

	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>

	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
	<binary>00000000</binary>
</scope>